package com.cc.pay;

/**
 * @Description
 * @Author 蔡超[of3379]
 * @date 2021.02.18 18:54
 */

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradePrecreateResponse response = Payment.FaceToFace()
                    .preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功"+response.toString());
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com";
        config.signType = "RSA2";

        config.appId = "2016092400586753";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDz+c28/XqoNNgT6zycsb4CK5FY+9pEX3F9H8Kb4qciBabTjEDQvrIz1pI2W2czLARvTw7MPFH8mD+viMgiK0XXUVF/tQHWhVmAgdQN8Ien8uFIHek7vhukiY/eD3OS9suwwnPaZIzt7ZG3lUJKLwpCmoz3TrIMzOmEE778NsZIkdoXMMyFr4sT1cQA6jBfKYmh5yBZpeYd2lcHj78s6XFHeHcXfwWN2hWxTl1CV+tskB25t1kmUFoJZZxlhQpk6hbwhR/USreuzPc75J9fguP4/nfQCmAu8iftgUIpRSI5K1Js3+EjhAiVpRI3zzGidaMaDPEodnULNyrjFv6iEgGVAgMBAAECggEAIG6sstrfW0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aSnyCyJ60mjC3YSz/atJ/73ami1k8AoNzNzzI/mXrjEa/yDMUzXx+NAJKYOGLGBR+YTy7s3veH9+8jUlK71d4stamraL1vUvRIQjsuY3vz8ZwHx19wxVwF2XxWS9gEac5yP7A==";

        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
//        config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
//        config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
//        config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
         config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxG/xBiEUEe/A/+kM7CBT86t9reBIpo3DrQJI5wMvfNQg7E/AL2d3yAjZpo83+BGPSoXV8KzeI5RGM/l6CN+6j0wIW0RG5wK8e4bjjFAZAz+DhvWDWUZw7GGPxfg/q20oNyaZCQ4SCWy2VyK95qR1h1gDmnIQSx3ZT2LTT112El2zkBgcB47CruG/wT9423tS4NSccTM6g/pI6fmvLlDWjNBmQFwIgUQHwVPGQWMSqmROWqVeslKDzmQTC5VzlBZxSiTjuSEa7g4UgNeKzqD4W2E7bf8LqRHAxwFOTb/yaP1W41F90OLvIAVtejFHIq3O6gkQG5dR2ftcvgRQRXLmqQIDAQAB";

//        //可设置异步通知接收服务地址（可选）
//        config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";
//
//        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
//        config.encryptKey = "<-- 请填写您的AES密钥，例如：aa4BtZ4tspm2wnXLb1ThQA== -->";

        return config;
    }
}